package com.trxy.hotelsystem.contorller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trxy.hotelsystem.model.clientInf;
import com.trxy.hotelsystem.model.room;
import com.trxy.hotelsystem.model.vip_management;
import com.trxy.hotelsystem.service.impl.RoomserviceImpl;
import com.trxy.hotelsystem.service.impl.VipServiceImpl;
import com.trxy.hotelsystem.service.impl.Vip_managementServiceImpl;

import frame.uitls.PrimaryKeyUtil;
import net.sf.json.JSONArray;

@Controller
public class VipController {
	
	@Autowired
	private VipServiceImpl VipserviceImpl;
	
	@Autowired
	private RoomserviceImpl Roomserviceimpl;
	
	@Autowired
	private Vip_managementServiceImpl vip_managementServiceImpl;
	
	//会员查询
	@RequestMapping(value = "/queryVipAll.do")
	public String queryVipAll(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		List<clientInf> list = VipserviceImpl.queryVipAll();
		
		 PageInfo<clientInf> page = new PageInfo<>(list);

		 model.addAttribute("pageInfo", page);
		

			return "view/vip";
	}
	
	//会员查询
	@RequestMapping(value = "/queryVipAll2.do")
	public String queryVipAll2(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		List<clientInf> list = VipserviceImpl.queryVipAll();
		
		 PageInfo<clientInf> page = new PageInfo<>(list);

		 model.addAttribute("pageInfo", page);
		

			return "view/vipCheckIn";
	}
	
	//多条件查询会员
	@RequestMapping(value = "/selectClient.do")
	public String selectClient(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);

		//获取参数
		String clientNumber = request.getParameter("clientNumber");
		String clientfName = request.getParameter("clientfName");
		String clientIdnum = request.getParameter("clientIdnum");
		String clientNumber2 = clientNumber.trim();
		String clientfName2 = clientfName.trim();
		String clientIdnum2 = clientIdnum.trim();
		if(clientNumber2 != "") {
			
			client.setNumber(clientNumber2);
		}
		if(clientfName2 != "") {
			
			client.setName(clientfName2);
		}
		if(clientIdnum2 != "") {
			
			client.setIdnumber(clientIdnum2);
		}
		
		//访问service
		List<clientInf> list = VipserviceImpl.selectVipByother(client);
		PageInfo<clientInf> page = new PageInfo<>(list);
		
		model.addAttribute("pageInfo", page);
		model.addAttribute("clientNumber2", clientNumber2);
		model.addAttribute("clientfName2", clientfName2);
		model.addAttribute("clientIdnum2", clientIdnum2);
		model.addAttribute("inf", "多条件");
		
		return "view/vip";
	}
	
	
	//添加会员
	@RequestMapping(value = "/addVip.do")
	public String addVip(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client) throws UnsupportedEncodingException, ParseException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String clientName = request.getParameter("clientName");
		String clientgender = request.getParameter("clientgender");
		String clientage = request.getParameter("clientage");
		String clientId = request.getParameter("clientId");
		String clientAddress = request.getParameter("clientAddress");
		String clientbrith = request.getParameter("clientbrith");
		String clientphone = request.getParameter("clientphone");
		String clientmoney = request.getParameter("clientmoney");
		String clientRe = request.getParameter("clientRe");
		
		//装箱
		client.setId(PrimaryKeyUtil.getPrimaryKey());
		client.setName(clientName);
		client.setGender(clientgender);
		client.setAge(Integer.parseInt(clientage));
		String str;
		while(true) {
			str =String.valueOf(Math.round(Math.random()*1000000));
			List<clientInf> list = VipserviceImpl.selectVipBynumber(str);
			if(list.size() == 0) {
				if(str.length() == 6) {
					client.setNumber(str);
					break;
				}
				
			}
		}
		client.setIdnumber(clientId);
		client.setPassword("000000");
		client.setAddress(clientAddress);
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");  
		Date date = format1.parse(clientbrith);  
		client.setBirth(date);
		client.setPhone(clientphone);
		client.setMoney(Double.valueOf(clientmoney));
		client.setRemark(clientRe);
		client.setGrade("普通会员");
		
		int i = VipserviceImpl.insertVip(client);
		
		if(i > 0) {//插入成功
			model.addAttribute("successful", client.getNumber());
			return "view/addStafSuccess";
		}else {//插入失败
			
			return "view/addStfdefeated";
		}
		
	}
	
	//员工号查询会员
	@RequestMapping(value = "selectvipByNumber.do")
	public String selectvipByNumber(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String vipNumber = request.getParameter("number");
		
		
		List<clientInf> list = VipserviceImpl.selectVipBynumber(vipNumber);
		
		model.addAttribute("client", list);
		
		return "view/addVip";
	}
	
	//删除会员
	@RequestMapping(value = "deleteVip.do")
	public void deleteVip(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String id = request.getParameter("id");
		
		int i = VipserviceImpl.deleteVip(id);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
	}
	
	//员工号查询会员——————ajax
	@RequestMapping(value = "selectvipByNumber2.do")
	public void selectvipByNumber2(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String vipNumber = request.getParameter("number");
		
		List<clientInf> list = VipserviceImpl.selectVipBynumber(vipNumber);
		for(clientInf one:list) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        String brith = sdf.format(one.getBirth());
	        one.setReserved1(brith);
			
		}

		//JSON格式返回数据
		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);	
	}
	
	//修改会员信息
	@RequestMapping(value = "updateVip.do")
	public void updateVip(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String vipName = request.getParameter("vipName");
		String vipgender = request.getParameter("vipgender");
		String vipage = request.getParameter("vipage");
		String vipidnumber = request.getParameter("vipidnumber");
		String vipaddress = request.getParameter("vipaddress");
		String vipphone = request.getParameter("vipphone");
		String vipremark = request.getParameter("vipremark");
		String vipNumber = request.getParameter("vipNumber");
		
		client.setName(vipName);
		client.setGender(vipgender);
		client.setAge(Integer.parseInt(vipage));
		client.setIdnumber(vipidnumber);
		client.setAddress(vipaddress);
		client.setPhone(vipphone);
		client.setRemark(vipremark);
		client.setNumber(vipNumber);
		
		int i = VipserviceImpl.updeVipselect(client);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}

	}
	
	//预约房间
	@RequestMapping(value = "subscribeRoom.do")
	public void subscribeRoom(HttpServletRequest request, HttpServletResponse response,
			HttpSession session,room rom,clientInf client) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String roomNumber = request.getParameter("roomNumber");
		
		clientInf us = (clientInf) session.getAttribute("viplogin");
		
		rom.setReserved2(us.getNumber());
		rom.setStatus("已预约");
		rom.setRoomnum(Integer.parseInt(roomNumber));
		
		int i = Roomserviceimpl.updeRoomByroomnumber(rom);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}	
	}
	
	//查看预约房间
	@RequestMapping(value = "/querySubscribeRoom.do")
	public String querySubscribeRoom(HttpServletRequest request, HttpServletResponse response,
			HttpSession session,room rom,Model model,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		clientInf us = (clientInf) session.getAttribute("viplogin");
		
		List<room> list = Roomserviceimpl.querySubscribeRoom(us.getNumber());
		
		 PageInfo<room> page = new PageInfo<>(list);

		 model.addAttribute("pageInfo", page);
		
		return "view/vipview/subscribeRoom";
	}
	
	//会员取消预约房间
	@RequestMapping(value = "cancelSubscribe.do")
	public void cancelSubscribe(HttpServletRequest request, HttpServletResponse response,
			HttpSession session,room rom) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String roomNumber = request.getParameter("roomNumber");
		
		rom.setStatus("空闲");
		rom.setRoomnum(Integer.parseInt(roomNumber));
		
		int i = Roomserviceimpl.cancelSubscribe(rom);
		
		if(i > 0) {
			
			pw.write("1");
		}else {
			
			pw.write("0");
		}
	}
	
	
	//会员号查询会员--会员登录
	@RequestMapping(value = "selectvipBysession.do")
	public String selectvipBysession(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client,HttpSession session) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		clientInf us = (clientInf) session.getAttribute("viplogin");
		
		
		clientInf list = VipserviceImpl.selectVipBynumber2(us.getNumber());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		list.setReserved3(sdf.format(list.getBirth()));
		
		model.addAttribute("client", list);
		
		return "view/vipview/alterVipInf";
	}
	
	
	//修改密码
	@RequestMapping(value = "changePassword.do")
	public void changePassword(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client,HttpSession session) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String passWord = request.getParameter("passWord");
		
		clientInf us = (clientInf) session.getAttribute("viplogin");
		
		client.setNumber(us.getNumber());
		client.setPassword(passWord);
		
		int i = VipserviceImpl.updeVipselect(client);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
		
	}
	
	//会员充值
	@RequestMapping(value = "vipTopup.do")
	public void vipTopup(HttpServletRequest request, HttpServletResponse response,
			Model model,clientInf client,clientInf clien,HttpSession session,vip_management vip_manag) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String topup = request.getParameter("topup");
		
		clientInf us = (clientInf) session.getAttribute("viplogin");
		
		client.setNumber(us.getNumber());
		client.setMoney(Double.parseDouble(topup)+us.getMoney());
		
		vip_management vipman = vip_managementServiceImpl.selectBygrade(us.getGrade());
		
		List<vip_management> list = vip_managementServiceImpl.queryVipmanagementAll();
		//会员充值升级判断
		for(vip_management one:list) {
			
			if(Integer.parseInt(topup) > one.getUpgrade()) {
				if(vipman.getSerialnumber() > one.getSerialnumber()) {
					
				}else {
					clien.setGrade(one.getGrade());
					clien.setNumber(us.getNumber());
					VipserviceImpl.updeVipselect(clien);
				}
			}
		}
		
		int i = VipserviceImpl.updeVipselect(client);
		
		if(i > 0) {
			us.setMoney(Double.parseDouble(topup)+us.getMoney());
			session.setAttribute("viplogin", us);
			session.setMaxInactiveInterval(60*60*60);
			pw.write("1");
		}else {
			pw.write("0");
		}
		
	}
	
	//查询会员管理表
	@RequestMapping(value = "queryVipmanagementAll.do")
	public String queryVipmanagementAll(HttpServletRequest request, HttpServletResponse response,
			Model model,vip_management vip_management) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		List<vip_management> list = vip_managementServiceImpl.queryVipmanagementAll();
		
		model.addAttribute("list", list);
		
		return "view/vip/vipmanagement";
	}
	
	//通过id查询会员升级表
	@RequestMapping(value = "selectvip_managementByid.do")
	public void selectvip_managementByid(HttpServletRequest request, HttpServletResponse response,
			Model model,vip_management vip_management) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String id = request.getParameter("id");
		
		vip_management vipman = vip_managementServiceImpl.selectByPrimaryKey(id);
		
		ArrayList<vip_management> list = new ArrayList<vip_management>();
		
		list.add(vipman);
		
		//JSON格式返回数据
				String st = JSONArray.fromObject(list).toString();
				response.getWriter().write(st);	
		
	}
	
	//修改会员升级管理表
	@RequestMapping(value = "amendVipMan.do")
	public void amendVipMan(HttpServletRequest request, HttpServletResponse response,
			Model model,vip_management vip_management) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String id = request.getParameter("id");
		String grade = request.getParameter("grade");
		String discounts = request.getParameter("discounts");
		String upgrade = request.getParameter("upgrade");
		
		vip_management.setId(id);
		vip_management.setGrade(grade);
		vip_management.setDiscounts(Double.parseDouble(discounts));
		vip_management.setUpgrade(Double.parseDouble(upgrade));
		
		int i = vip_managementServiceImpl.updateByPrimaryKeySelective(vip_management);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
		
		
		
	}
	
	
	

}
